package com.dzl.fee;

import com.dzl.base.controller.BaseController;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeOrderDetail;
import com.dzl.fee.pojo.query.WelfareFeeOrderQuery;
import com.dzl.fee.pojo.query.WelfareFeeUserCustomersSalesRelQuery;
import com.dzl.fee.pojo.vo.WelfareFeeOrderVo;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.fee.service.WelfareFeeUserCustomersSalesRelService;
import com.dzl.integral.pojo.WelfareIntegralRecord;
import com.dzl.integral.service.WelfareIntegralRecordService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 业务员订单 - 控制层
 * @author WangJian
 * @date 2021/8/5 0005
 */
@RestController
@RequestMapping("/salesOrder")
public class SalesOrderController extends BaseController {

    @Autowired
    private WelfareFeeOrderService welfareFeeOrderService;
    @Autowired
    private WelfareFeeUserCustomersSalesRelService welfareFeeUserCustomersSalesRelService;
    @Autowired
    private WelfareIntegralRecordService welfareIntegralRecordService;

    /**
     * 分页查询业务员订单列表
     * @date 2021/8/5 0005 15:42
     * @author wangjian
     * @param query 业务员订单查询条件
     * @return com.dzl.util.PageUtils<com.dzl.feeSalesOrder.pojo.vo.WelfareFeeOrderVo>
     */
    @GetMapping("/getFeeOrderList")
    public PageUtils<WelfareFeeOrderVo> getFeeOrderList(WelfareFeeOrderQuery query){
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareFeeOrderVo> pageInfo = welfareFeeOrderService.getFeeOrderListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 根据客户ID查业务员信息，根据业务员D查客户信息
     * @date 2021/8/5 0005 16:45
     * @author wangjian
     * @param query 客户和业务员查询条件
     * @return com.dzl.util.JsonResult<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @GetMapping("/queryCustomerSalesRel")
    public JsonResult<Map<String, Object>> queryCustomerSalesRel(WelfareFeeUserCustomersSalesRelQuery query){
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        return JsonResult.success(welfareFeeUserCustomersSalesRelService.selectCustomersSalesRelVoList(query));
    }

    /**
     * 查询回款记录
     * @date 2021/8/6 0006 15:01
     * @author wangjian
     * @param id 业务员订单ID
     * @return com.dzl.util.JsonResult<com.dzl.feeSalesOrder.pojo.vo.WelfareFeeOrderVo>
     */
    @GetMapping("/getFeeOrderDetail")
    public JsonResult<WelfareFeeOrderVo> getFeeOrderDetail(Integer id){
        return JsonResult.success(welfareFeeOrderService.getFeeOrderDetail(id));
    }

    /**
     * 修改订单总金额
     * @date 2021/8/6 0006 15:05
     * @author wangjian
     * @param welfareFeeOrder 业务员订单对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateOrderAmount")
    public JsonResult<Integer> updateOrderAmount(@RequestBody WelfareFeeOrder welfareFeeOrder){
        List<WelfareFeeOrderDetail> orderDetailList = welfareFeeOrderService.getFeeOrderDetail(welfareFeeOrder.getId()).getWelfareFeeOrderDetailList();
        if (EmptyUtils.isNotEmpty(orderDetailList)){
            return JsonResult.failWithMsg("该订单有回款登记数据，暂无法修改");
        }
        WelfareFeeOrderVo vo = new WelfareFeeOrderVo();
        vo.setId(welfareFeeOrder.getId());
        vo.setTotalAmount(welfareFeeOrder.getTotalAmount());
        vo.setTotalAmountReturn(BigDecimal.ZERO);
        vo.setTotalAmountUnreturn(welfareFeeOrder.getTotalAmount());
        return JsonResult.success(welfareFeeOrderService.updateAmount(vo));
    }

    /**
     * 回款登记
     * @date 2021/8/6 0006 15:23
     * @author wangjian
     * @param vo 回款登记信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateOrderDetail")
    public JsonResult<Integer> updateOrderDetail(@RequestBody WelfareFeeOrderVo vo){
        if (vo.getTotalAmount() == null){
            return JsonResult.failWithMsg("请先设置订单金额");
        }else if (EmptyUtils.isEmpty(vo.getTotalAmount())){
            return JsonResult.failWithMsg("订单金额不能为0");
        }
        if (vo.getTotalAmountUnreturn() == null){
            return JsonResult.failWithMsg("请先设置未回款金额");
        }else if (vo.getTotalAmountUnreturn().compareTo(new BigDecimal(0)) == -1){
            return JsonResult.failWithMsg("未回款金额为负数，请修改回款金额");
        }
        BigDecimal totalAmountReturn = new BigDecimal(0);
        List<WelfareFeeOrderDetail> orderDetailList = vo.getWelfareFeeOrderDetailList();
        if (EmptyUtils.isNotEmpty(orderDetailList)){
            for (WelfareFeeOrderDetail detail :orderDetailList){
                if (EmptyUtils.isEmpty(detail.getAmount())){
                    return JsonResult.failWithMsg("回款金额不能为0");
                }else if (detail.getAmount().compareTo(new BigDecimal(0)) == -1){
                    return JsonResult.failWithMsg("回款金额不能小于0");
                }
                if (EmptyUtils.isEmpty(detail.getPayType()) || (detail.getPayType() != 1 && detail.getPayType() !=2)){
                    return JsonResult.failWithMsg("请正确设置支付类型");
                }
                if (EmptyUtils.isEmpty(detail.getUpdateTime())){
                    return JsonResult.failWithMsg("请选择回款时间");
                }
                //计算回款金额
                totalAmountReturn = totalAmountReturn.add(detail.getAmount());
                detail.setOrderId(vo.getId());
                detail.setCreaterName(getAdminInfo().getUserName());
                detail.setStatus(1);
            }
        }
        vo.setTotalAmountReturn(totalAmountReturn);
        if (vo.getTotalAmount().compareTo(vo.getTotalAmountUnreturn().add(totalAmountReturn))!=0){
            return JsonResult.failWithMsg("订单总金额必须等于回款金额加未回款金额");
        }
        return JsonResult.success(welfareFeeOrderService.updateOrderDetail(vo));
    }

    /**
     * 订单转移
     * @date 2021/8/6 0006 15:51
     * @author wangjian
     * @param welfareFeeOrder  业务员订单对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateSalesAndCustomers")
    public JsonResult<Integer> updateSalesAndCustomers(@RequestBody WelfareFeeOrder welfareFeeOrder){
        if (EmptyUtils.isNotEmpty(welfareFeeOrder.getOrderType()) && welfareFeeOrder.getOrderType() == 3){
            if (EmptyUtils.isEmpty(welfareFeeOrder.getEnterpriseId())){
                return JsonResult.failWithMsg("请选择saas客户");
            }
            //查询saas客户充值积分的状态，只有在待审核期间才能进行订单转移
            WelfareIntegralRecord welfareIntegralRecord = new WelfareIntegralRecord();
            welfareIntegralRecord.setStatus(1);
            welfareIntegralRecord.setFeeOrderId(welfareFeeOrder.getId());
            int i = welfareIntegralRecordService.selectCount(welfareIntegralRecord);
            if (i == 0) {
                return JsonResult.failWithMsg("该笔积分充值不是待审核状态，无法进行订单转移");
            }
        }else {
            if (EmptyUtils.isEmpty(welfareFeeOrder.getCustomersId())){
                return JsonResult.failWithMsg("请选择客户");
            }
        }
        if (EmptyUtils.isEmpty(welfareFeeOrder.getSalesUserId())){
            return JsonResult.failWithMsg("请选择业务员");
        }
        WelfareFeeOrder feeOrder = new WelfareFeeOrder();
        feeOrder.setId(welfareFeeOrder.getId());
        feeOrder.setSalesUserId(welfareFeeOrder.getSalesUserId());
        feeOrder.setCustomersId(welfareFeeOrder.getCustomersId());
        feeOrder.setEnterpriseId(welfareFeeOrder.getEnterpriseId());
        return JsonResult.success(welfareFeeOrderService.updateByPrimaryKeySelective(feeOrder));
    }


}
